package com.zmn.oms.task.job.orderwork;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.dto.work.modify.OrderTurnDTO;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkBService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 类描述：订单自动转单
 *
 * @author liuying
 * @date 2019/07/16 16:04
 */
@LTS
@Slf4j
public class OrderWorkAutoTurnJob {

    /**
     * 禁止转单的产品
     */
    private static List<Integer> PRODUCT_BLACK_LIST = Lists.newArrayList(10141, 10140, 10119, 10145, 10104, 10098, 10099, 10101, 10317, 10313);
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private ZsOrderWorkBService zsOrderWorkBService;
    @Autowired
    private WorkFlowContextBService workFlowContextBService;
    /*
    以下产品，均不可以自动转单
    家电维修 > 数码产品 > 电脑 > 电脑维修 10141
    家电维修 > 数码产品 > 手机 > 手机维修 10140
    家电维修 > 生活电器 > 吸尘器 > 吸尘器维修 10119
    家电维修 > 办公设备 > 打印机 > 打印机维修 10145
    家电维修 > 厨房电器 > 电磁炉 > 电磁炉维修 10104
    家电维修 > 厨房电器 > 电饭煲 > 电饭煲维修 10098
    家电维修 > 厨房电器 > 料理机 > 料理机维修 10099
    家电维修 > 厨房电器 > 微波炉 > 微波炉维修 10101
    房屋修缮 > 防水补漏 > 防水补漏 > 屋顶防水补漏 10317
    房屋修缮 > 防水补漏 > 防水补漏 > 卫生间防水补漏 10313
    */

    /**
     * 自动转单
     *
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_AUTO_TRRN)
    public Result runAutoTurnJob(Job job) {

        log.info("#order#autoTurn 执行：" + job);

        String orderIdStr = job.getParam("orderId");
        if (!NumberUtil.isNumber(orderIdStr)) {
            return new Result(Action.EXECUTE_FAILED, "订单号错误!");
        }

        Long orderId = Long.valueOf(orderIdStr);
        try {
            Boolean result = zsOrderWorkBService.saveAutoTurn(orderId);
            // 转单后校验
            Boolean flag = zsOrderWorkBService.checkTurnStatus(orderId,orderId);
            if(!flag){
                OrderTurnDTO orderTurnDTO = new OrderTurnDTO();
                orderTurnDTO.setOrderId(orderId);
                orderTurnDTO.setWorkId(orderId);
                orderTurnDTO.setStatus(OrderStatusConsts.WORK_STATUS_INPUT);
                orderTurnDTO.setResultStatus(OrderStatusConsts.WORK_RESULT_DOING);
                orderTurnDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
                orderTurnDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
                zsOrderWorkBService.changeTurnStatus(orderTurnDTO);
                log.debug("#order#autoTurn {} 转单后，已确认->已录入", orderId);
            }
            if (result) {
                // 工单自动流转
                log.debug("#order#autoTurn {} asyncAutoFlow", orderId);
                workFlowContextBService.asyncAutoFlow(orderId, orderId);
                return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
            }
        } catch (Exception e) {
            log.error("#order#autoTurn 自动转单异常 {} {}", e.getMessage(), e);
            return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
          }
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

}
